package com.heba.fragment.message;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.heba.Cfg;
import com.heba.bean.notice.ItemMessageNoticeObject;
import com.heba.bean.notice.ItemNoticeObject;
import com.heba.bean.notice.UserNoticeObject;
import com.heba.fragment.LazyFragment;
import com.heba.greenmulu.R;
import com.heba.greenmulu.message.ItemNotificationDiaryActivity;
import com.heba.greenmulu.message.ItemNotificationFriendActivity;
import com.heba.greenmulu.message.ItemNotificationLikeActivity;
import com.heba.greenmulu.message.ItemNotificationReviewActivity;
import com.heba.helper.DateHelper;
import com.heba.helper.HttpClientHelper;
import com.heba.helper.JSONHttpHelper;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 通訊錄通知頁面
 * Created by jason on 2020/1/7.
 */
public class MessageNotificationFragment extends LazyFragment {

    private SwipeRefreshLayout swipeRefreshLayout;
    private View view_diary;  //新日记通知
    private View view_friends;  //好友通知
    private View view_like;    //点赞通知
    private View view_review;  //评论通知

    private ItemMessageNoticeObject noticeObject;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_message_notification, container, false);
        initView(view);
        initClicklistener();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        swipeRefreshLayout.setRefreshing(true);
        getNoticeData();
    }

    /**
     * initView
     *
     * @param view
     */
    private void initView(View view) {
        swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
        view_diary = view.findViewById(R.id.view_diary);
        view_friends = view.findViewById(R.id.view_friends);
        view_like = view.findViewById(R.id.view_like);
        view_review = view.findViewById(R.id.view_review);

        /**
         * 下拉刷新操作
         */
        swipeRefreshLayout.setColorSchemeResources(R.color.green);
        swipeRefreshLayout.setOnRefreshListener(() -> getNoticeData());
    }

    /**
     * initClicklistener
     */
    private void initClicklistener() {
        view_diary.setOnClickListener(v -> openActivity(ItemNotificationDiaryActivity.class));
        view_friends.setOnClickListener(v -> openActivity(ItemNotificationFriendActivity.class));
        view_like.setOnClickListener(v -> openActivity(ItemNotificationLikeActivity.class));
        view_review.setOnClickListener(v -> openActivity(ItemNotificationReviewActivity.class));
    }

    /**
     * 设置item信息
     *
     * @param view
     * @param type              通知類型
     * @param imageID
     * @param notificationTitle
     */
    private void findItemView(View view, int type, int imageID, String notificationTitle, ItemNoticeObject object) {
        ImageView iv_icon = view.findViewById(R.id.iv_icon);
        TextView tv_notification = view.findViewById(R.id.tv_notification);
        TextView tv_time = view.findViewById(R.id.tv_time);
        TextView tv_nums = view.findViewById(R.id.tv_nums);
        TextView tv_content = view.findViewById(R.id.tv_content);

        tv_nums.setVisibility(object.getIsNew() == 1 ? View.VISIBLE : View.INVISIBLE);
        iv_icon.setImageResource(imageID);
        tv_notification.setText(notificationTitle);
        tv_time.setText(DateHelper.transferDateString(getActivity(), object.getTimeCreated()));
        tv_nums.setText(object.getCount() + "");

        String userName = "";
        if (object.getUserList() != null && !object.getUserList().isEmpty()) {
            UserNoticeObject userNoticeObject = object.getUserList().get(0);
            userName = userNoticeObject.getAccount();
        }

        /**
         * 顯示內容
         */
        switch (type) {
            case 1:
                tv_content.setText(String.format(getActivity().getResources().getString(R.string.message_diary_text), userName, object.getCount()));
                break;
            case 2:
                tv_content.setText(String.format(getActivity().getResources().getString(R.string.message_friend_text), object.getCount()));
                break;
            case 3:
                tv_content.setText(String.format(getActivity().getResources().getString(R.string.message_like_text), userName, object.getCount()));
                break;
            case 4:
                tv_content.setText(String.format(getActivity().getResources().getString(R.string.message_review_text), userName, object.getCount()));
                break;
            default:
                break;
        }

        if (object.getCount() == 0)
            tv_content.setText("");

    }

    /**
     * 获取各种通知数据
     */
    private void getNoticeData() {
        HttpClientHelper client = new HttpClientHelper();
        client.setUseLogin(true);
        RequestParams params = new RequestParams();
        params.put("Leng", Cfg.Leng);
        client.get(Cfg.BaseUrl + "/Notice/Panel", params,
                new JSONHttpHelper.JSONHttpResponseHandler(getActivity()) {

                    @Override
                    public void Success() {
                        // TODO Auto-generated method stub
                        super.Success();
                        if (swipeRefreshLayout.isRefreshing())
                            swipeRefreshLayout.setRefreshing(false);
                        try {
                            JSONObject jsonObject = new JSONObject(response.trim()).getJSONObject("Result");
                            noticeObject = new Gson().fromJson(jsonObject.toString(), ItemMessageNoticeObject.class);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        /**
                         * 显示数据
                         */
                        if (null != noticeObject) {
                            findItemView(view_diary, 1, R.drawable.icon_message_diary, getResources().getString(R.string.message_diary), noticeObject.getDiray());
                            findItemView(view_friends, 2, R.drawable.icon_message_friend, getResources().getString(R.string.message_friend), noticeObject.getFriend());
                            findItemView(view_like, 3, R.drawable.icon_message_like, getResources().getString(R.string.message_like), noticeObject.getLike());
                            findItemView(view_review, 4, R.drawable.icon_message_comments, getResources().getString(R.string.message_review), noticeObject.getReply());
                        }

                    }

                    public void Failure() {
                        super.Failure();
                        if (swipeRefreshLayout.isRefreshing())
                            swipeRefreshLayout.setRefreshing(false);
                    }

                });
    }

}
